<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      // 降序和升序
      const arr = [
        {
          id: 1,
          age: 20,
          name: "jack",
        },
        {
          id: 2,
          age: 21,
          name: "lili",
        },
        {
          id: 3,
          age: 18,
          name: "lucy",
        },
        {
          id: 4,
          age: 22,
          name: "tom",
        },
      ];
      arr.sort(function (a, b) {
        // return a.age > b.age ? 1 : -1;
        return a.name < b.name ? 1 : -1;
      });
      console.log("原数组", arr);
    </script>
  </body>
</html>
